﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using System.Collections.Generic;
using System.Data;

namespace zevenseas.ClassicTheme
{
    public partial class CustomThemes : LayoutsPageBase
    {
        

        protected override void CreateChildControls()
        {
            ThemesXmlFileLocations themesXmlFileLocations = webApplicationSelector.CurrentItem.GetChild<ThemesXmlFileLocations>("ThemesXmlFileLocations");

            if (themesXmlFileLocations != null)
            {
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("FilePaths");

                foreach (string fileLocation in themesXmlFileLocations.FilePaths)
                {
                    DataRow row = dataTable.NewRow();
                    row["FilePaths"] = fileLocation;
                    dataTable.Rows.Add(row);
                }

                gridView.DataSource = dataTable;
                gridView.DataBind();
            }
            base.CreateChildControls();
        }

        public void OnClickOK(object sender, EventArgs e)
        {
            //SPFarm farm = SPFarm.Local;

            ThemesXmlFileLocations themesXmlFileLocations = webApplicationSelector.CurrentItem.GetChild<ThemesXmlFileLocations>("ThemesXmlFileLocations");

            // If no settings previously created, create them now.    
            if (themesXmlFileLocations == null)
            {
                SPPersistedObject parent = webApplicationSelector.CurrentItem;
                themesXmlFileLocations = new ThemesXmlFileLocations("ThemesXmlFileLocations", parent, Guid.NewGuid());
                themesXmlFileLocations.Update();
            }

            List<string> fileLocations = themesXmlFileLocations.FilePaths;
            if (fileLocations == null)
            {
                fileLocations = new List<string>();
            }

            if (!string.IsNullOrEmpty(uxTheme.Text))
            { fileLocations.Add(uxTheme.Text); }


            themesXmlFileLocations.FilePaths = fileLocations;
            themesXmlFileLocations.Update();

            SPUtility.Redirect(this.PageToRedirectOnCancel, SPRedirectFlags.Default, this.Context);

        }

        public void DeleteLink(object sender, EventArgs e)
        {
            SPFarm farm = SPFarm.Local;
            ThemesXmlFileLocations themesXmlFileLocations = webApplicationSelector.CurrentItem.GetChild<ThemesXmlFileLocations>("ThemesXmlFileLocations");

            string[] filePaths = null;
            filePaths = RetrieveIDs();

            if (filePaths != null)
            {
                foreach (string filePath in filePaths)
                {
                    if (!string.IsNullOrEmpty(filePath))
                    {
                        themesXmlFileLocations.FilePaths.Remove(filePath);
                        themesXmlFileLocations.Update();
                    }
                }
            }
        }

        private string[] RetrieveIDs()
        {
            string[] guids = null;
            if (((base.Request != null) && (base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"] != null)) && !string.IsNullOrEmpty(base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"]))
            {
                guids = base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"].ToString().Split(new Char[] { '#' });
            }
            return guids;
        }   
    }
}
